//
//  RecoverPasswordController.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/5/28.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import UIKit

class RecoverPasswordController: CBBaseViewController {
    
    @IBOutlet weak var tfMobile: UITextField!
    
    @IBOutlet weak var tfCode: UITextField!
    
    @IBOutlet weak var tfNewPassword: UITextField!
    
    @IBOutlet weak var btnGetSmsCode: UIButton!
    
    @IBOutlet weak var btnSet: UIButton!
    
    @IBOutlet weak var lblTips: UILabel!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "找回密码"
        
        let recoverVm = RecoverPasswordViewModel(mobile: tfMobile.rx.text.orEmpty.asObservable(), code: tfCode.rx.text.orEmpty.asObservable(), newPassword: tfNewPassword.rx.text.orEmpty.asObservable(), sendSmsTap: btnGetSmsCode.rx.tap.asObservable(), setTap: btnSet.rx.tap.asObservable())
        
        recoverVm.valid
            .subscribe(onNext:{ [weak self] result in
                self?.btnSet.isEnabled = result.0
                self?.btnSet.alpha = result.0 ? 1.0 : 0.5
                self?.lblTips.text = result.1
            }).disposed(by: disposeBag)
        
        //发送短信验证码
        recoverVm.validMobile
            .bind(to: btnGetSmsCode.rx.isEnabled)
            .disposed(by: disposeBag)
        recoverVm.sendSms
            .subscribe(onNext:{ [unowned self] isSendCode in
                if isSendCode {
                    self.btnGetSmsCode.isEnabled = false
                    self.stopTimer()
                    self.setSmsBtnTitle(title: "\(self.count)")
                    self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.tickTime), userInfo: nil, repeats: true)
                }
            }).disposed(by: disposeBag)
        
        //重置操作
        recoverVm.setPwd
            .subscribe(onNext:{ [weak self] isSuccess in
                if isSuccess {
                    self?.navigationController?.popViewController(animated: true)
                }
            }).disposed(by: disposeBag)
        
        //屏幕点击释放
        let viewTap = UITapGestureRecognizer()
        viewTap.rx.event.subscribe(onNext:{ [unowned self] _ in
            self.view.endEditing(true)
        }).disposed(by: disposeBag)
        view.addGestureRecognizer(viewTap)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        stopTimer()
    }
    
    var timer:Timer?
    var count:Int = 60
    @objc func tickTime() {
        count -= 1
        setSmsBtnTitle(title: "\(count)")
        if count <= 0 { stopTimer() }
    }
    
    func stopTimer() {
        if self.timer != nil {
            self.timer?.invalidate()
            self.timer = nil
            count = 60
            setSmsBtnTitle(title: "获取验证码")
            btnGetSmsCode.isEnabled = true
        }
    }
    
    func setSmsBtnTitle(title:String) {
        self.btnGetSmsCode.setTitle(title, for: .normal)
        self.btnGetSmsCode.setTitleColor(UIColor(hex:0x2f8fe1), for: .normal)
    }


}
